// The payment page
// ================

define(['zepto', 'utils', 'wxupload'], function($, utils, wxupload) {
  return {

    init: function(data) {

      this.bindQrcodeUpload(data.jsConfig);
      
      this.bindSexSelect();
      
      this.bindMarriageSelect();

      this.bindSubmitAction();

    },

    bindQrcodeUpload : function(config) {

      var uploadButtin = $("a.upload-image")
      
      uploadButtin.on('click', function(e){
          wxupload.injectConfig(config)
          wxupload.invokeUpload(config)
      })

    },
    
    bindSexSelect: function(sites) {
        
      that = this
      
      var $sexSelector = $("input[name='sexSelector']")
      
      $sexSelector.picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                            <button class="button button-link pull-right close-picker">确定</button>\
                            <h1 class="title">请选择性别</h1>\
                         </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['MALE', 'FEMALE', 'UNKNOW'],
            displayValues: ['男', '女', '未知']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).next().val(value)
          return displayValue
        }
      });
      
    },
    
    bindMarriageSelect: function(sites) {
      
      that = this
      
      var $marriageSelector = $("input[name='marriageSelector']")
      
      $marriageSelector.picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                            <button class="button button-link pull-right close-picker">确定</button>\
                            <h1 class="title">请选择婚姻状况</h1>\
                         </header>',
        cols: [
          {
            textAlign: 'center',
            values: ['UNMARRIED', 'MARRIED', 'CLASSIFIED'],
            displayValues: ['未婚', '已婚', '保密']
          }
        ],
        formatValue: function (picker, value, displayValue){
          $(this.input).next().val(value)
          return displayValue
        }
      });
      
    },

    // order submit evvent handler
    bindSubmitAction: function() {
      
      var thatSubmit = this
      
      var submitButton = $("a.button-submit")
      
      submitButton.on('click', function(e){
        e.preventDefault()
        // init all data
        var data = thatSubmit.onInitData()
        // validate all input
        if(thatSubmit.onValidateData(data)) {
          // do submit order with data
          data = thatSubmit.onAppendData(data)
          thatSubmit.onSubmitOrder(data)
        }
      })
      
    },

    // init data
    onInitData: function() {
      return {
        'orderId': {
          'value': $('input[name="orderId"]').val().trim(),
          'message': '订单不能为空'
        },
        'height': {
          'value': $('input[name="height"]').val().trim(),
          'message': '身高不能为空'
        },
        'weight': {
          'value': $('input[name="weight"]').val().trim(),
          'message': '请填写体重'
        },
        'age': {
          'value': $('input[name="age"]').val().trim(),
          'message': '请填写年龄'
        },
        'wechatNumber': {
          'value': $('input[name="wechatNumber"]').val().trim(),   
          'message': '手机号不能为空'
        },
        'marriage': {
          'value': $('input[name="marriage"]').val().trim(),   
          'message': '婚姻状况不能为空'
        },
        'sex': {
          'value': $('input[name="sex"]').val().trim(),   
          'message': '性别不能为空'
        }
      }
    },

    onValidateData: function(data) {
      var valid = _.find(data, function(item) {
        return !item.value
      })

      if (!!valid) return $.toast(valid.message)

      return true
    },

    onAppendData: function(data) {
      
      data.expectations = {
        'value': $('textarea[name="expectations"]').text()
      }

      data.source = {
        'value': $('input[name="source"]:checked').val()
      }
      
      return data;
    },

    onSubmitOrder: function(data) {

      $.showPreloader('正在提交')

      var processedData = _.mapObject(data, function(obj) {
        return obj.value
      })
      $.ajax({
        type: 'POST',
        url: __CTX__.root + '/shop/investigation',
        data: processedData
      }).done(function(response) {
        $.hidePreloader()
        utils.processResult(response, function(result) {
          $.toast('填写调查表成功')
          setTimeout(function() {
            window.location.href = __CTX__.root + '/profile'
          }, 1000)
        }, function(result) {
          $.toast(result.message || '填写调查表失败')
        })
      })
      
    }

  }
})